package com.weilive.core.utils;

import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailSender {

	/**
	 * 邮件认证器
	 */
	public class MailAuthenticator extends Authenticator {
		public final String username;
		public final String password;

		public MailAuthenticator(String username, String password) {
			this.username = username;
			this.password = password;
		}

		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	}
	
	/**
	 * 成员对象
	 */

	private final Properties props = new Properties();
	private final MailAuthenticator authenticator;
	private final Session session;

	/**
	 * 初始化
	 * @param username 发送邮件的用户名(地址)
	 * @param password 密码
	 * @param smtpHostName SMTP主机地址
	 */
	public MailSender(String smtpHostName, int smtpHostPort, String username, String password) {
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtpHostName);
		props.put("mail.smtp.port", "" + smtpHostPort);
		props.put("mail.transport.protocol","smtp");
		authenticator = new MailAuthenticator(username, password);
		session = Session.getInstance(props, authenticator);
	}
	
	public MailSender(String smtpHostName, String username, String password) {
		this(smtpHostName, 25, username, password);
	}

	public MailSender(String username, String password) {
		//通过邮箱地址解析出smtp服务器，对大多数邮箱都管用
		this("smtp." + username.split("@")[1], username, password);
	}

	/**
	 * 发送邮件
	 * @param recipient 收件人邮箱地址
	 * @param subject 邮件主题
	 * @param content 邮件内容
	 */
	public void send(String recipient, String subject, String content) throws AddressException, MessagingException {
		MimeMessage message = new MimeMessage(session);	// 创建mime类型邮件
		message.setFrom(new InternetAddress(authenticator.username));	// 设置发信人
		message.setRecipient(RecipientType.TO, new InternetAddress(recipient));	// 设置收件人
		message.setSubject(subject, "utf-8");	// 设置主题
		message.setContent(content, "text/html;charset=utf-8");	// 设置邮件内容
		Transport.send(message);	// 发送
	}

	/**
	 * 群发邮件
	 * @param recipients 收件人们
	 * @param subject 主题
	 * @param content 内容
	 */
	public void send(List<String> recipients, String subject, String content) throws AddressException, MessagingException {
		MimeMessage message = new MimeMessage(session);	// 创建mime类型邮件
		message.setFrom(new InternetAddress(authenticator.username));	// 设置发信人
		final int num = recipients.size();	// 设置收件人们
		InternetAddress[] addresses = new InternetAddress[num];
		for (int i = 0; i < num; i++) {
			addresses[i] = new InternetAddress(recipients.get(i));
		}
		message.setRecipients(RecipientType.TO, addresses);
		message.setSubject(subject, "utf-8");	// 设置主题
		message.setContent(content, "text/html;charset=utf-8");	// 设置邮件内容
		Transport.send(message);	// 发送
	}
	
	/**
	 * test
	 */
	
	public static void main(String[] args) throws Exception {
		MailSender emailSender = new MailSender("smtp.qq.com", "company@lemonade-game.com", "lemonade2014");
		emailSender.send("herohook@163.com", "《泰时明月》找回密码", "<a href=\"http://www.theganker.com\">点击此处</a>获取新密码，获得新密码后，请立马登录，并修改!");
	}
}
